package com.woniu.product.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import com.woniu.product.client.ProductQueryClient;
import com.woniu.product.client.param.PageQuerySpuByTypeParam;
import com.woniu.product.client.param.ListCrowdProductParam;
import com.woniu.product.client.param.PageQuerySpuParam;
import com.woniu.product.client.param.SearchAllParam;
import com.woniu.product.client.result.*;
import com.woniu.product.infra.model.Sku;
import com.woniu.product.infra.model.Spu;
import com.woniu.product.service.SkuService;
import com.woniu.product.service.SpuService;
import com.woniu.product.service.dto.PageCrowdProductDto;
import com.woniu.product.service.dto.ProductItemDto;
import com.woniu.product.service.dto.SpuDto;
import com.woniu.product.service.form.ListCrowdProductForm;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@RestController
@Slf4j
public class ProductQueryApiImpl implements ProductQueryClient {

    @Resource
    private SkuService skuService;

    @Resource
    private SpuService spuService;
    @Resource
    private ElasticsearchOperations eo;

    @Override
    public Result search(SearchAllParam searchAllParam) {
        log.info("es搜索:{}", searchAllParam);
        return spuService.search(searchAllParam);
    }

    /**
     * 通过skuIds批量查询Skus详情
     *
     * @param skuIds
     * @return
     */
    @Override
    public Result<List<SkuResultInfo>> batchQuerySkus(List<Integer> skuIds) {
        log.info("通过skuIds批量查询Skus详情:{}", skuIds);
        Collection<Sku> skus = skuService.listByIds(skuIds);
        List<SkuResultInfo> skuResultInfos = BeanUtil.copyToList(skus, SkuResultInfo.class);
        return Result.getInstanceList(SkuResultInfo.class).setData(skuResultInfos);
    }

    /**
     * 根据spuId查询Spu详情
     *
     * @param spuId
     * @return
     */
    @Override
    public Result<SpuResultInfo> getSpuInfoBySpuId(Integer spuId,Integer userId) {
        log.info("根据spuId查询Spu详情：{}{}", spuId,userId);
        SpuDto spuDto = spuService.querySpuInfo(spuId,userId);
        SpuResultInfo spuResultInfo = BeanUtil.copyProperties(spuDto, SpuResultInfo.class);
        return Result.getInstance(SpuResultInfo.class).setData(spuResultInfo);
    }

    /**
     * 根据属性Ids查询Sku详情
     *
     * @param propValueIds
     * @return
     */
    @Override
    public Result<SkuResultInfo> getSkuInfoByPropValueIds(List<Integer> propValueIds) {
        log.info("根据属性Ids查询Sku详情:{}", propValueIds);
        //对ids排序
        Collections.sort(propValueIds);
        //用-连接
        String ids = Joiner.on('-').join(propValueIds);
        QueryWrapper<Sku> qw = new QueryWrapper<>();
        qw.eq("quick_query_code", ids);
        Sku sku = skuService.getOne(qw);
        SkuResultInfo skuResultInfo = BeanUtil.copyProperties(sku, SkuResultInfo.class);
        return Result.getInstance(SkuResultInfo.class).setData(skuResultInfo);
    }

    /**
     * 根据skuId查询Sku详情
     *
     * @param skuId
     * @return
     */
    @Override
    public Result<SkuResultInfo> getSkuInfoBySkuId(Integer skuId) {
        log.info("根据skuId查询Sku详情:{}", skuId);
        Sku sku = skuService.getById(skuId);
        SkuResultInfo skuResultInfo = BeanUtil.copyProperties(sku, SkuResultInfo.class);
        return Result.getInstance(SkuResultInfo.class).setData(skuResultInfo);
    }

    /**
     * 根据分类类别分页查询Spus
     *
     * @param pageQuerySpuParam
     * @return
     */
    @Override
    public Result<PageSpuResultInfo> pageQuerySpusByCategoryId(PageQuerySpuParam pageQuerySpuParam) {
        log.info("根据分类类别分页查询Spus:{}", pageQuerySpuParam);
        QueryWrapper<Spu> qw = new QueryWrapper<>();
        qw.eq("spu_status", "ON_SALE");
        if (pageQuerySpuParam.getCategoryId() != null) qw.eq("category_id", pageQuerySpuParam.getCategoryId());
        IPage<Spu> spus = spuService.page(new Page<>(pageQuerySpuParam.getIndexPage(), pageQuerySpuParam.getPageSize()), qw);
        PageSpuResultInfo pageSpuResultInfo = new PageSpuResultInfo();
        pageSpuResultInfo.setSpus(BeanUtil.copyToList(spus.getRecords(), SpuResultInfo.class));
        pageSpuResultInfo.setIndexPage(spus.getCurrent());
        pageSpuResultInfo.setTotal(spus.getTotal());
        return Result.getInstance(PageSpuResultInfo.class).setData(pageSpuResultInfo);
    }

    /**
     * 根据skuId查询商品的信息
     *
     * @param skuId
     * @return
     */
    @Override
    public Result<ProductItemResultInfo> getProductInfoBySkuId(Integer skuId) {
        log.info("根据skuId查询商品的信息:{}", skuId);
        ProductItemDto productItemDto = skuService.queryProductItem(skuId);
        ProductItemResultInfo productItemResultInfo = BeanUtil.copyProperties(productItemDto,ProductItemResultInfo.class);
        return Result.getInstance(ProductItemResultInfo.class).setData(productItemResultInfo);
    }

    @Override
    public Result<PageSpuResultInfo> pageQuerySpusByType(PageQuerySpuByTypeParam pageQuerySpuByTypeParam) {
        log.info("根据商品类别查询:{}", pageQuerySpuByTypeParam);
        QueryWrapper<Spu> qw = new QueryWrapper<>();
        qw.eq("spu_status", "ON_SALE");
        if (pageQuerySpuByTypeParam.getSpuType() != null) qw.eq("spu_type", pageQuerySpuByTypeParam.getSpuType());
        IPage<Spu> spus = spuService.page(new Page<>(pageQuerySpuByTypeParam.getIndexPage(), pageQuerySpuByTypeParam.getPageSize()), qw);
        PageSpuResultInfo pageSpuResultInfo = new PageSpuResultInfo();
        pageSpuResultInfo.setSpus(BeanUtil.copyToList(spus.getRecords(), SpuResultInfo.class));
        pageSpuResultInfo.setIndexPage(spus.getCurrent());
        pageSpuResultInfo.setTotal(spus.getTotal());
        return Result.getInstance(PageSpuResultInfo.class).setData(pageSpuResultInfo);
    }

    @Override
    public Result pageListProductCrowd(ListCrowdProductParam listCrowdProductParam) {
        log.info("分页查询众筹商品:{}", listCrowdProductParam);
        ListCrowdProductForm listCrowdProductForm = BeanUtil.copyProperties(listCrowdProductParam, ListCrowdProductForm.class);
        PageCrowdProductDto pageCrowdProductDto = spuService.pageListProductCrowd(listCrowdProductForm);
        PageCrowdProductResultInfo pageCrowdProductResultInfo = BeanUtil.copyProperties(pageCrowdProductDto, PageCrowdProductResultInfo.class);
        return Result.getInstance(PageCrowdProductResultInfo.class).setMsg("查询成功").setCode(200).setData(pageCrowdProductResultInfo);
    }


}
